<?php
namespace org\crawlgenie\models;
use org\crawlgenie\models\dao\JobListingMetaDAO;
use org\crawlgenie\application\environment\Request;
use \Logger;
use \CGException;


/**
 * Description of JobListingMeta
 * This is the class for JobListing metadata, 
 * meant to update the repos_metadata table
 * 
 *  @author $Author: jinsonxu@gmail.com $:
 *  $Date: 2014-11-14 05:59:34 +0000 (Fri, 14 Nov 2014) $:
 *  $Id: JobListingMeta.php 15 2014-11-14 05:59:34Z jinsonxu@gmail.com $:
 *  @version $Rev: 15 $:
 *  @package crawlgenie.models
 *  @subpackage dao
 */

class JobListingMeta extends BaseModel {

    protected $id, $batchID, $instanceID, $author, $crawlURI, $resourceType, $path, 
        $characterSet, $fileSize, $acl, $dateCreated, $creator, 
        $lastModified, $pageCount, $title, $body, $content;
    
    public function __construct($id = null) {
        $this->id = $id;
        $this->author = CG_NAME . '_' . Request::getParam('mod');        
        $this->lastModified = date('Y-m-d H:i:sO');  // set by default to the current date timestamp
        $this->pageCount = 1;
        $this->creator = null;
        $this->acl = null;
        $this->resourceType = 'web';  // by default, cos job listings usually available online as http resources
        
    }

/*
    public function __get($name) {

        if(array_key_exists($name, $this->dataset)) {
           return $this->dataset[$name];
        } else {
            $trace = debug_backtrace();
            throw new CGException('Undefined property via __get(): ' . $name .
                ' in ' . $trace[0]['file'] .
                ' on line ' . $trace[0]['line'],
                E_USER_NOTICE);
        }
    }
*/

    public function store() {
        JobListingMetaDAO::store($this);
    }


    
    public function load() {
        if($this->id || ($this->batchID && $this->author && $this->crawlURI)) {
            
            if(JobListingMetaDAO::metaObjExists($this->id)) {
                $metaObj = JobListingMetaDAO::load($this->id);
                $this->acl = $metaObj->getAcl();
                $this->author = $metaObj->getAuthor();
                $this->batchID = $metaObj->getBatchID();
                $this->body = $metaObj->getBody();
                $this->characterSet = $metaObj->getCharacterSet();
                $this->crawlURI = $metaObj->getCrawlURI();
                $this->creator = $metaObj->getCreator();
                $this->dateCreated = $metaObj->getDateCreated();
                $this->fileSize = $metaObj->getFileSize();
                $this->id = $metaObj->getId();
                $this->lastModified = $metaObj->getLastModified();
                $this->pageCount = $metaObj->getPageCount();
                $this->path = $metaObj->getPath();
                $this->resourceType = $metaObj->getResourceType();
                $this->title = $metaObj->getTitle();

            } else {
                Logger::getLogger(__CLASS__)->info(__METHOD__ . ': Unable to load JobListingMeta object ' . $this->id . '. Does not exist in DB yet.');
            }

        } else {
            throw new CGException('Unable to load JobListingMeta object due to missing metaID in dataset');
        }
    }


    public function exists() {
        if($this->id) {
            return JobListingMetaDAO::metaObjExists($this->id);
            
        } else if($this->batchID && $this->author && $this->crawlURI) {
            return JobListingMetaDAO::metaObjExists2($this->batchID, $this->author, $this->crawlURI);
            
        } else {
            return false;
        }
    }


    

    public function getId() {
        return $this->id;
    }

    public function getBatchID() {
        return $this->batchID;
    }

    public function getInstanceID() {
        return $this->instanceID;
    }

    public function getAuthor() {
        return $this->author;
    }

    public function getCrawlURI() {
        return $this->crawlURI;
    }

    public function getResourceType() {
        return $this->resourceType;
    }

    public function getPath() {
        return $this->path;
    }

    public function getCharacterSet() {
        return $this->characterSet;
    }

    public function getFileSize() {
        return $this->fileSize;
    }

    public function getAcl() {
        return $this->acl;
    }

    public function getDateCreated() {
        return $this->dateCreated;
    }

    public function getCreator() {
        return $this->creator;
    }

    public function getLastModified() {
        return $this->lastModified;
    }

    public function getPageCount() {
        return $this->pageCount;
    }

    public function getTitle() {
        return $this->title;
    }

    public function getBody() {
        return $this->body;
    }
    
    public function getContent() {
        return $this->content;
    }

    public function setId($id) {
        $this->id = $id;
    }
    
    /**
     * Batch ID refers to the batch crawling identifier for 
     * data obtained within a specific time period. 
     * e.g. Jobs data crawled on a fortnightly basis may have a batchID
     * comprising of the YEARMONTH.MONTHHALVE i.e. 201411.1 / 201411.2
     * 
     * @param type $batchID
     */
    public function setBatchID($batchID) {
        $this->batchID = $batchID;
    }

    /**
     * Instance ID is a more specific identifier for the crawl that specifies
     * the exact YEARMONTHDAYHOURMINUTESECOND that the crawl was initialized.
     * @param type $instanceID
     */
    public function setInstanceID($instanceID) {
        $this->instanceID = $instanceID;
    }

    public function setAuthor($author) {
        $this->author = null!==$author ? trim($author) : null;
    }

    public function setCrawlURI($crawlURI) {
        $this->crawlURI = null!==$crawlURI ? trim($crawlURI) : null;
    }

    public function setResourceType($resourceType) {
        $this->resourceType = null!==$resourceType ? trim($resourceType) : null;
    }

    public function setPath($path) {
        $this->path = null!==$path ? trim($path) : null;
    }

    public function setCharacterSet($characterSet) {
        $this->characterSet = $characterSet;
    }

    public function setFileSize($fileSize) {
        $this->fileSize = $fileSize;
    }

    public function setAcl($acl) {
        $this->acl = $acl;
    }

    public function setDateCreated($dateCreated) {
        $this->dateCreated = $dateCreated;
    }

    public function setCreator($creator) {
        $this->creator = $creator;
    }

    public function setLastModified($lastModified) {
        $this->lastModified = $lastModified;
    }

    public function setPageCount($pageCount) {
        $this->pageCount = $pageCount;
    }

    public function setTitle($title) {
        $this->title = null!==$title ? trim($title) : $title;
    }

    public function setBody($body) {
        $this->body = null!==$body ? trim($body) : $body;
    }

    public function setContent($content) {
        $this->content = null!== $content ? trim($content) : $content;
        $this->characterSet = mb_detect_encoding($content, 'auto');  // set char encoding by default.
    }
}
?>